package com.only4play.user.web.resources;

import com.only4play.common.model.GeneralResponse;
import com.only4play.user.model.dto.output.UserPersonalAccountDto;
import com.only4play.user.service.UserPersonalAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file PermissionResource
 * @brief PermissionResource
 * @details PermissionResource
 * @date 2024/12/14
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2024/12/14                    liyuncong          Created
 */
@RestController
@RequestMapping(
    value = "/api/v1/authorizeActions",
    produces = MediaType.APPLICATION_JSON_VALUE
)
@RequiredArgsConstructor
public class AuthorizeActionResource {

    @Autowired
    private UserPersonalAccountService accountService;

    @PostMapping(
        path = "/authenticate",
        consumes = MediaType.APPLICATION_JSON_VALUE
    )
    public GeneralResponse<UserPersonalAccountDto> authenticate(
        @RequestBody Map<String, String> authBody
    ) {
        String username = authBody.get("username");
        String password = authBody.get("password");
        return GeneralResponse.of(
            accountService.authenticate(username, password).orElse(null)
        );
    }
}
